<?php

namespace frontend\controllers;

use Yii;
use yii\web\Controller;
use yii\web\UploadedFile;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\Config;
use yii\helpers\ArrayHelper;

/**
 * Config controller
 */
class ConfigController extends Controller {

    /**
     * @inheritdoc
     */
    public function behaviors() {
        if (Config::getLicense()['sensorNowCount'] == 0) {
            $rules = [
                [
                    'actions' => ['getbase', 'setbase'],
                    'allow' => true,
                    'roles' => ['admin'],
                ],
            ];
        } else {
            $rules = [
                [
                    'actions' => [''],
                    'allow' => true,
                    'roles' => ['?'],
                ],
                [
                    'actions' => ['getloophole', 'getbase'],
                    'allow' => true,
                    'roles' => ['@'],
                ],
                [
                    'actions' => [],
                    'allow' => true,
                    'roles' => ['admin'],
                ],
            ];
        }
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['getloophole', 'setloophole', 'getbase', 'setbase'],
                'rules' => $rules,
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                // 'logout' => ['post'],
                // 'test' => ['post'],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function actions() {
        $this->isAPI();
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    /**
     * Displays homepage.
     *
     * @return mixed
     */
    public $enableCsrfValidation = false;

    private function isAPI() {
        $headers = Yii::$app->request->headers;
        if (stristr($headers['accept'], 'application/json') !== false) {
            Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        } else {
            Yii::$app->response->format = \yii\web\Response::FORMAT_XML;
        }
    }

    public function actionGetloophole() {
        $data = Config::getLoophole();
        $data['status'] = 'success';
        return $data;
    }

    public function actionSetloophole() {
        if (!Yii::$app->request->isPost) {
            $data['status'] = 'fail';
            $data['errorMessage'] = 'Not post request';
            return $data;
        }
        if (Yii::$app->request->isPost) {
            $RawBody = trim(Yii::$app->request->getRawBody());
            $post = json_decode($RawBody, true);
        }
        if (empty($post)) {
            $data['status'] = 'fail';
            $data['errorMessage'] = 'Rawbody not JSON.';
            return $data;
        }
        Config::setLoophole($post);
        $data = Config::getLoophole();
        $data['status'] = 'success';
        return $data;
    }

    public function actionGetbase() {
        $data = Config::getBase();
        $data['status'] = 'success';
        return $data;
    }

    public function actionSetbase() {
        if (!Yii::$app->request->isPost) {
            $data['status'] = 'fail';
            $data['errorMessage'] = 'Not post request';
            return $data;
        }
        if (Yii::$app->request->isPost) {
            $RawBody = trim(Yii::$app->request->getRawBody());
            $post = json_decode($RawBody, true);
        }
        if (empty($post)) {
            $data['status'] = 'fail';
            $data['errorMessage'] = 'Rawbody not JSON.';
            return $data;
        }
        $data = Config::getBase();
        foreach ($post as $key => $value) {
            $data[$key] = $value;
        }
        Config::setBase($data);
        $data = Config::getBase();
        $data['status'] = 'success';
        return $data;
    }

}
